package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.pojo.Draft;
import com.systemic.pojo.Dynamic;
import com.systemic.service.DraftService;
import com.systemic.vo.DraftInfoVo;
import com.systemic.vo.DraftVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: DraftController
 * @Description: 草稿箱
 * @Author:
 * @CreateTime: 2024-01-05 13:57
 * @Version:
 **/

@RestController
@RequestMapping("/draft")
public class DraftController {

    @Resource
    private DraftService draftService;


    /**
     * 获取用户的草稿箱列表
     * @param uid
     * @return
     */
    @GetMapping("/list/{uid}")
    public ResponseResult<List<DraftVo>> getDraftList(@PathVariable("uid") Integer uid){
        List<DraftVo> draftVoList = draftService.selectListByUid(uid);
        if (draftVoList.isEmpty()){
            return ResponseResult.failed("没有查询到用户的草稿");
        }
        return ResponseResult.success(draftVoList);
    }

    /**
     * 获取草稿具体信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public ResponseResult<DraftInfoVo> getDraftInfo(@PathVariable("id") Integer id){
        DraftInfoVo draftInfoVo = draftService.selectById(id);
        if (draftInfoVo==null){
            return ResponseResult.failed("没有查询到用户的草稿");
        }
        return ResponseResult.success(draftInfoVo);
    }

    /**
     * 删除草稿
     * @param draft
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult<String> delete(@RequestBody Draft draft){
        int row = draftService.delete(draft);
        if (row==0){
            return ResponseResult.failed("删除草稿失败");
        }
        return ResponseResult.success("删除草稿成功");
    }

    /**
     * 更新草稿，在客户端中，无论修改了哪个草稿，都是一起发送给服务端，
     * 服务端需要做的是把草稿对应的原有的图片删除，删除当前草稿id对应所有表记录重新新增。
     * @param images
     * @param id
     * @param content
     * @param category
     * @return
     * @throws IOException
     */
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestParam("images") MultipartFile[] images,
                                         @RequestParam("id") Integer id,
                                         @RequestParam("content") String content,
                                         @RequestParam("category") Integer category,
                                         @RequestParam("subject") Integer[] subject) throws IOException {

        int row = 0;
        if (content!=null){
            if (category==1){
                Draft draft = new Draft();
                draft.setId(id);
                draft.setContent(content);
                draft.setCategory(category);
                row = draftService.update(images, draft, subject);
            } else {
                System.out.println("类型错误");
            }
        }
        if (row==0){
            return ResponseResult.failed("操作失败！");
        }
        return ResponseResult.success("操作成功！");
    }


    /**
     * 草稿转动态
     * @param draft
     * @return
     */
    @PostMapping("/push")
    public ResponseResult<String> push(@RequestBody Draft draft){
        int row = draftService.push(draft);
        if (row==0){
            return ResponseResult.failed("发布动态失败");
        }
        return ResponseResult.success("发布动态成功");
    }


}
